#include "GameManager.h"
#include "InputManager.h"
#include "../GameObjects/Player.h"
#include "../Maps/TerrainType.h"
/*#include "../Maps/Forest.h"
#include "../Maps/Weather.h"*/
//#include "../World/World.h"
#include "../World2/TheWorld.h"
#include "../Renderer/DeferredRenderer.h"

#include <iostream>

GameManagerPtr GameManager::singleton;

const int w = 90;
const int h = 60;

GameManagerPtr GameManager::getSingleton()
{
	if(singleton == nullptr)
		singleton = std::make_shared<GameManager>();
	return singleton;
}

GameManager::GameManager()
	: quit_(false)
{
}

GameManager::~GameManager()
{
}

void GameManager::init(int argc, char **argv)
{
	std::cout << "Do you want to run in fullscreen mode? y) yes, n) no." << std::endl;
	char input;
	std::cin >> input;
	bool fullscreen = false;
	if(input == 'y')
		fullscreen = true;

	std::string base_dir(argv[0]);
	base_dir = base_dir.substr(0, base_dir.find_last_of("\\"));
	base_dir = base_dir.substr(0, base_dir.find_last_of("\\"));
	base_dir += "\\resources\\";
	
	TCODConsole::setCustomFont("terminal.png", TCOD_FONT_LAYOUT_ASCII_INROW | TCOD_FONT_TYPE_GREYSCALE, 16,16);
	TCODConsole::initRoot(w, h, "Trefall's Roguelike", fullscreen, TCOD_RENDERER_GLSL);
	TCODConsole::root->setAlignment(TCOD_CENTER);
	TCODConsole::root->clear();

	map_viewport = Vec2i(w, h);
	story_viewport = Vec2i(w, 20);
	map_console = std::make_shared<TCODConsole>(map_viewport.x(), map_viewport.y());
	story_console = std::make_shared<TCODConsole>(story_viewport.x(), story_viewport.y());
	story_console->setAlignment(TCOD_LEFT);

	map_console->clear();
	story_console->clear();

	//Construct
	renderer = std::make_shared<DeferredRenderer>(map_viewport);
	input_mgr = std::make_shared<InputManager>();
	player = std::make_shared<Player>(map_viewport);
	terrain_type_mgr = std::make_shared<TerrainTypeManager>();
	//forest = std::make_shared<Forest>(400, 400);
	//weather = std::make_shared<Weather>();

	//world = std::make_shared<World>();

	world = std::make_shared<World::TheWorld>();

	//Initialize
	player->init();
	//world->init(map_viewport);
	//forest->init();
	world->generate(base_dir+"world\\");
	player->setPos(180, 120);

	input_mgr->setKeyCallback(TCODK_ESCAPE, 0, [this](TCOD_key_t input) { this->quit(); });
	input_mgr->setKeyCallback(TCODK_F1, 0, [this](TCOD_key_t input) { TCODSystem::saveScreenshot(nullptr); });
}

void GameManager::run()
{
	while(TCODConsole::isWindowClosed() == false && quit_ == false)
	{
		story_console->clear();
		float dt = TCODSystem::getLastFrameLength();
		
		renderer->prepare();

		input_mgr->handleKeys();
		//weather->update(dt);
		//forest->update();
		player->update();
		//world->update();

		//Build G-Buffer
		//forest->render();
		world->render();
		player->render();

		//Render deferred result
		renderer->render();
		
		TCODConsole::blit(map_console.get(), 0, 0, w, h, TCODConsole::root, 0,0);
		//TCODConsole::blit(story_console.get(), 0, 0, w, 20, TCODConsole::root, 0,h-20);
		TCODConsole::flush();
	}
}

